package gene.android.accountingpro;

import gene.android.accountingpro.equations.AccountsReceivableTurnoverRatio;
import gene.android.accountingpro.equations.CurrentRatio;
import gene.android.accountingpro.equations.DebtToTotalAssetsRatio;
import gene.android.accountingpro.equations.DebtToTotalEquityRatio;
import gene.android.accountingpro.equations.EarningsPerShare;
import gene.android.accountingpro.equations.Equation;
import gene.android.accountingpro.equations.GrossProfitMarginRatio;
import gene.android.accountingpro.equations.InventoryTurnoverRatio;
import gene.android.accountingpro.equations.NetProfitOnSalesRatio;
import gene.android.accountingpro.equations.QuickRatio;
import gene.android.accountingpro.equations.ReturnOnAssetsRatio;
import gene.android.accountingpro.equations.ReturnOnEquityRatio;
import gene.android.accountingpro.equations.TimesInterestEarnedRatio;
import gene.android.accountingpro.finals.EquationNames;
import gene.android.accountingpro.finals.FieldList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ShowSolvedEquations extends Activity {

	public static final String CLASS_TAG = ShowSolvedEquations.class.getSimpleName();

	private ArrayList<String> requiredEquationNames;
	private List<Equation> requiredEquations;
	private HashMap<String, Double> fieldNameValueMap;

	private LinearLayout container;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showsolvedequations);

		container = (LinearLayout)findViewById(R.id.showsolvedequationscontainer);

		requiredEquationNames = getIntent().getExtras().getStringArrayList(FormGenerator.REQUIRED_EQUATIONS);
		fieldNameValueMap = (HashMap<String, Double>) getIntent().getExtras().getSerializable(FormGenerator.FIELD_NAMES_VALUE_MAP);
		requiredEquations = EquationNames.equationNamesListToEquationsList(requiredEquationNames);

		for( Equation equation: requiredEquations )
		{
			container.addView( solveEquation( equation ) );
		}

	}


	public LinearLayout solveEquation( Equation equation )
	{

		if( equation instanceof CurrentRatio )
		{
			CurrentRatio cr = (CurrentRatio) equation;
			cr.setValues( new double[]{ fieldNameValueMap.get(FieldList.CURRENT_ASSETS),
					fieldNameValueMap.get(FieldList.CURRENT_LIABILITES)});
			return createSolvedEquationTextView( cr.getEquationName(), cr.getSolvedEquationString() );
		}

		if( equation instanceof QuickRatio )
		{
			QuickRatio qr = (QuickRatio) equation;
			qr.setValues( new double[]{ fieldNameValueMap.get(FieldList.CASH),
					fieldNameValueMap.get(FieldList.SHORT_TERM_INVESTMENTS),
					fieldNameValueMap.get(FieldList.ACCOUNTS_RECIEVABLE),
					fieldNameValueMap.get(FieldList.CURRENT_LIABILITES)});
			return createSolvedEquationTextView( qr.getEquationName(), qr.getSolvedEquationString() );

		}
		if( equation instanceof DebtToTotalAssetsRatio )
		{
			DebtToTotalAssetsRatio dttar = (DebtToTotalAssetsRatio) equation;
			dttar.setValues( new double[]{ fieldNameValueMap.get(FieldList.TOTAL_DEBT),
					fieldNameValueMap.get(FieldList.TOTAL_ASSETS)});
			return createSolvedEquationTextView( dttar.getEquationName(), dttar.getSolvedEquationString() );

		}
		if( equation instanceof DebtToTotalEquityRatio )
		{
			DebtToTotalEquityRatio dtter = (DebtToTotalEquityRatio) equation;
			dtter.setValues( new double[]{ fieldNameValueMap.get(FieldList.TOTAL_DEBT),
					fieldNameValueMap.get(FieldList.TOTAL_EQUITY)});
			return createSolvedEquationTextView( dtter.getEquationName(), dtter.getSolvedEquationString() );

		}
		if( equation instanceof TimesInterestEarnedRatio )
		{
			TimesInterestEarnedRatio tier = (TimesInterestEarnedRatio) equation;
			tier.setValues( new double[]{ fieldNameValueMap.get(FieldList.INCOME_BEFORE_INCOME_TAXES_AND_INTEREST),
					fieldNameValueMap.get(FieldList.INTEREST_CHARGES )});
			return createSolvedEquationTextView( tier.getEquationName(), tier.getSolvedEquationString() );

		}
		if( equation instanceof AccountsReceivableTurnoverRatio )
		{
			AccountsReceivableTurnoverRatio artr = (AccountsReceivableTurnoverRatio) equation;
			artr.setValues( new double[]{ fieldNameValueMap.get(FieldList.NET_CREDIT_SALES),
					fieldNameValueMap.get(FieldList.AVERAGE_NET_ACCOUNTS_RECEIVABLE )});
			return createSolvedEquationTextView( artr.getEquationName(), artr.getSolvedEquationString() );

		}

		if( equation instanceof InventoryTurnoverRatio )
		{
			InventoryTurnoverRatio itor = (InventoryTurnoverRatio) equation;
			itor.setValues( new double[]{ fieldNameValueMap.get(FieldList.COST_OF_GOODS_SOLD),
					fieldNameValueMap.get(FieldList.AVERAGE_INVENTORY )});
			return createSolvedEquationTextView( itor.getEquationName(), itor.getSolvedEquationString() );

		}
		if( equation instanceof NetProfitOnSalesRatio )
		{
			NetProfitOnSalesRatio npos = (NetProfitOnSalesRatio) equation;
			npos.setValues( new double[]{ fieldNameValueMap.get(FieldList.NET_INCOME),
					fieldNameValueMap.get(FieldList.NET_SALES )});
			return createSolvedEquationTextView( npos.getEquationName(), npos.getSolvedEquationString() );

		}
		if( equation instanceof GrossProfitMarginRatio )
		{
			GrossProfitMarginRatio gpmr = (GrossProfitMarginRatio) equation;
			gpmr.setValues( new double[]{ fieldNameValueMap.get(FieldList.GROSS_PROFIT),
					fieldNameValueMap.get(FieldList.NET_SALES )});
			return createSolvedEquationTextView( gpmr.getEquationName(), gpmr.getSolvedEquationString() );

		}
		if( equation instanceof ReturnOnAssetsRatio )
		{
			ReturnOnAssetsRatio roar = (ReturnOnAssetsRatio) equation;
			roar.setValues( new double[]{ fieldNameValueMap.get(FieldList.NET_INCOME),
					fieldNameValueMap.get(FieldList.INTEREST_EXPENSE ),
					fieldNameValueMap.get(FieldList.AVERAGE_ASSETS )});
			return createSolvedEquationTextView( roar.getEquationName(), roar.getSolvedEquationString() );

		}
		if( equation instanceof ReturnOnEquityRatio )
		{
			ReturnOnEquityRatio roer = (ReturnOnEquityRatio) equation;
			roer.setValues( new double[]{ fieldNameValueMap.get(FieldList.NET_INCOME),
					fieldNameValueMap.get(FieldList.PREFERRED_DIVIDENDS ),
					fieldNameValueMap.get(FieldList.AVERAGE_COMMON_EQUITY )});
			return createSolvedEquationTextView( roer.getEquationName(), roer.getSolvedEquationString() );

		}
		if( equation instanceof EarningsPerShare )
		{
			EarningsPerShare eps = (EarningsPerShare) equation;
			eps.setValues( new double[]{fieldNameValueMap.get(FieldList.INCOME_AVAILABLE_TO_COMMON ),
					fieldNameValueMap.get(FieldList.WEIGHTED_AVERAGE_NUMBER_OF_COMMON_SHARES )});
			return createSolvedEquationTextView( eps.getEquationName(), eps.getSolvedEquationString() );

		}

		return null;

	}


	private LinearLayout createSolvedEquationTextView( String label, String solvedEquation )
	{
		LinearLayout layout = new LinearLayout( this );
		layout.setOrientation(LinearLayout.VERTICAL);
		TextView labelTextView = new TextView( this );
		labelTextView.setText( label );
		labelTextView.setTextSize( 20 );
		layout.addView( labelTextView );
		TextView solvedEquationTextView = new TextView( this );
		solvedEquationTextView.setText( solvedEquation );
		layout.addView( solvedEquationTextView );
		return layout;
	}



}
